package com.gs.synedocket.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.gs.synedocket.R;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class ImageUtil {

    public static void loadImg(Context activity, String url, ImageView iv) {
        loadImg(activity,url,iv,R.drawable.ic_photo_error);
    }

    public static void loadImg(Context activity, byte[] url, ImageView iv) {
        loadImg(activity,url,iv,R.drawable.ic_photo_error);
    }

    public static void loadImg(Context activity, byte[] url, ImageView iv,int errorPhoto) {
        Glide.with(activity)
                .load(url)
                .error(errorPhoto)
                .into(iv);
    }

    public static void loadImg(Context activity, String url, ImageView iv,int errorPhoto) {
        Glide.with(activity)
                .load(url)
                .error(errorPhoto)
                .into(iv);
    }

    public static byte[] base64ToByte(String base64){
        return Base64.decode(base64,Base64.DEFAULT);
    }

    public static Bitmap getViewBitmap(View v) {
        v.clearFocus();
        v.setPressed(false);

        boolean willNotCache = v.willNotCacheDrawing();
        v.setWillNotCacheDrawing(false);

        int color = v.getDrawingCacheBackgroundColor();
        v.setDrawingCacheBackgroundColor(0);

        if (color != 0) {
            v.destroyDrawingCache();
        }

        v.buildDrawingCache();
        Bitmap cacheBitmap = v.getDrawingCache();
        if (cacheBitmap == null) {
            return null;
        }

        Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

        v.destroyDrawingCache();
        v.setWillNotCacheDrawing(willNotCache);
        v.setDrawingCacheBackgroundColor(0);
        return bitmap;
    }

    public static String bitmapToUrl(Context context, Bitmap bitmap) {
        FileOutputStream fops = null;
        String path;
        File saveFile = FileUtils.getInstance().createFile(context, "SynEDocket" + DateUtil.DateTimeToStr(new Date(), "yyyyMMdd_hhmmss") + ".jpg");
        path = saveFile.getAbsolutePath();
        try {
            fops = new FileOutputStream(path);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fops);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fops != null) {
                    fops.flush();
                    fops.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return path;
    }

    public static String image2Base64(String path) {
        if (TextUtils.isEmpty(path)) {
            return null;
        }
        InputStream is = null;
        byte[] data;
        String result = null;
        try {
            is = new FileInputStream(path);
            data = new byte[is.available()];
            is.read(data);
            result = Base64.encodeToString(data, Base64.NO_CLOSE);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    public static String bitmapToBase64(Bitmap bitmap) {
        String result = null;
        ByteArrayOutputStream baos = null;
        try {
            if (bitmap != null) {
                baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

                baos.flush();
                baos.close();

                byte[] bitmapBytes = baos.toByteArray();
                result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.flush();
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
